import { createStore, Store } from 'vuex';
import type { ActionContext } from 'vuex';
import type { InjectionKey } from 'vue';
// import {  } from 'vuex';

// 定义状态接口
interface State {
    isLoggedIn: boolean;
}

// 定义注入键
export const key: InjectionKey<Store<State>> = Symbol('store');

// 创建store实例
export const store = createStore<State>({
    state() {
        return {
            isLoggedIn: localStorage.getItem('isLoggedIn') === 'true'
        };
    },
    mutations: {
        // 登录状态变更
        setLoginState(state: State, status: boolean) {
            state.isLoggedIn = status;
            localStorage.setItem('isLoggedIn', status.toString());
        }
    },
    actions: {
        // 登录
        login({ commit }: ActionContext<State, State>) {commit('setLoginState', true)},
        // 登出
        logout({ commit }: ActionContext<State, State>) {commit('setLoginState', false)}
    },
    getters: {
        // 获取登录状态
        isAuthenticated(state: State) {
            return state.isLoggedIn;
        }
    }
})

